package com.momoauth.api_client.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.momoauth.api_client.entity.Organization;
import com.momoauth.api_client.mapper.OrganizationMapper;
import com.momoauth.common.core.result.ResponseResurt;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/organization")
@Api(tags = "组织")
public class OrganizationController {

    @Resource
    OrganizationMapper organizationMapper;

    /**
     * 组织列表
     * @param organization
     * @return
     */
    @ApiOperation(value = "组织列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页数", required = true)
    })
    @RequestMapping(value = "/getOrganizationList", method = RequestMethod.GET)
    public ResponseResurt getOrganizationtList(Organization organization) {
        QueryWrapper<Organization> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("isDelete", 0);
        queryWrapper.orderByDesc("orgId"); //按主键倒序排序
        queryWrapper.like(!StringUtils.isEmpty(organization.getKeywords()),"orgName", organization.getKeywords());
        queryWrapper.eq(organization.getStatus() != 0,"status", organization.getStatus());
        Page<Organization> page = new Page<>(organization.getCurrentPage(), organization.getPageSize());
        page.setRecords(organizationMapper.getOrganizationList(page, queryWrapper));
        return ResponseResurt.success().data(page);
    }

    /**
     * 创建组织
     * @param organization
     * @param bindingResult
     * @return
     */
    @ApiOperation(value = "创建组织")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgName", value = "组织名称", required = true),
            @ApiImplicitParam(name = "description", value = "描述", required = false),
            @ApiImplicitParam(name = "status", value = "状态", required = true)
    })
    @RequestMapping(value = "/createOrganization", method = RequestMethod.POST)
    public ResponseResurt createOrganization(
            @Validated Organization organization,
            BindingResult bindingResult
    ) {
        // 非法参数验证
        if(bindingResult.hasErrors()){
            String errorMsg = bindingResult.getFieldError().getDefaultMessage();
            return ResponseResurt.error().msg(errorMsg);
        }
        // 执行新增操作
        int isInsert = organizationMapper.insert(organization);
        if (isInsert > 0) {
            return ResponseResurt.success().msg("恭喜您，创建成功。");
        }
        return ResponseResurt.error().msg("很抱歉，创建失败。");
    }

    /**
     * 修改组织
     * @param organization
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/updateOrganization", method = RequestMethod.GET)
    @ApiOperation(value = "修改组织")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgId", value = "组织ID（主键）", required = true),
            @ApiImplicitParam(name = "orgName", value = "组织名称", required = true),
            @ApiImplicitParam(name = "description", value = "描述", required = false),
            @ApiImplicitParam(name = "status", value = "状态", required = true)
    })
    public ResponseResurt updateOrganization(
            @Validated Organization organization,
            BindingResult bindingResult
    ) {
        // 非法参数验证
        if(bindingResult.hasErrors()){
            String errorMsg = bindingResult.getFieldError().getDefaultMessage();
            return ResponseResurt.error().msg(errorMsg);
        }
        // 查询当前版本号
        long version = organizationMapper.selectById(organization.getOrgId()).getVersion();
        organization.setVersion(version);
        // 执行更新操作
        int isUpdate = organizationMapper.updateById(organization);
        if (isUpdate > 0) {
            return ResponseResurt.success().msg("恭喜您，修改成功。");
        }
        return ResponseResurt.error().msg("很抱歉，修改失败！");
    }

    /**
     * 删除组织
     * @param organization
     * @return
     */
    @ApiOperation(value = "删除组织")
    @ApiImplicitParam(name = "orgId", value = "组织ID（主键）", required = true)
    @RequestMapping(value = "/deleteOrganization", method = RequestMethod.GET)
    public ResponseResurt deleteOrganization(Organization organization) {
        // 执行更新操作（逻辑删除）
        int isUpdate = organizationMapper.deleteOrganizationById(organization.getOrgId());
        if (isUpdate > 0) {
            return ResponseResurt.success().msg("恭喜您，删除成功。");
        }
        return ResponseResurt.error().msg("很抱歉，删除失败！");
    }

    /**
     * 组织详情
     * @param organization
     * @return
     */
    @ApiOperation(value = "组织详情")
    @ApiImplicitParam(name = "orgId", value = "组织ID（主键）", required = true)
    @RequestMapping(value = "/getOrganizationById", method = RequestMethod.GET)
    public ResponseResurt getOrganizationById(Organization organization) {
        organization = organizationMapper.selectById(organization.getOrgId());
        return ResponseResurt.success().data(organization);
    }
}
